package com.yys.kit;

import java.util.concurrent.Callable;

public class RetryKit {

	public interface ResultCheck{
		
		boolean matching();
		
		String getJson();
	}
	
	public <V extends ResultCheck> V retryOnException(int retryLimit,Callable<V> retryCallable){
		
		V v = null;
		for(int i=0;i<retryLimit;i++){
			try {
				v = retryCallable.call();
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("retry on " + (i + 1) + " times v = " + (v == null ? null : v.getJson()) + "\n" + e);
			}
			if(v.matching())	break;
			System.out.println("retry on " + (i + 1) + " times but not matching . retult = " + (v == null ? null : v.getJson()));
		}
		
		return v;
	}
}
